package com.example.meituan.activity.user.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.meituan.R;

import java.util.ArrayList;
import java.util.List;

public class ImageSliderAdapter extends RecyclerView.Adapter<ImageSliderAdapter.ImageViewHolder> {


    private final Context context;
    private final List<String> images;

    public interface OnItemClickListener {
        void onRemoveClick(int position);
    }

    public ImageSliderAdapter(Context context, List<String> images) {
        this.context = context;
        this.images = images != null ? new ArrayList<>(images) : new ArrayList<>();
    }

    @NonNull
    @Override
    public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_image_slider, parent, false);
        return new ImageViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ImageViewHolder holder, int position) {

        if (images == null || position < 0 || position >= images.size()) {
            Glide.with(context)
                    .load(R.drawable.logo) // 替换为你的默认图片资源ID
                    .into(holder.imageView);
            return;
        }

        String imagePath = images.get(position);

        if (imagePath != null && !imagePath.isEmpty()) {
            try {
                Glide.with(context)
                        .load(imagePath)
                        .placeholder(R.drawable.photo)
                        .error(R.drawable.logo)
                        .into(holder.imageView);
            } catch (Exception e) {
                Glide.with(context)
                        .load(R.drawable.logo)
                        .into(holder.imageView);
            }
        } else {
            Glide.with(context)
                    .load(R.drawable.logo)
                    .into(holder.imageView);
        }
    }

    @Override
    public int getItemCount() {
        return images.size();
    }

    static class ImageViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;

        ImageViewHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.product_image);
        }
    }
}
